<?php
/**
 * Created by PhpStorm.
 * User: yafex
 * Date: 2019/10/29
 * Time: 14:54
 */
class core_ali extends L {

    /**
     * 描述 : 新增或修改1688的产品信息
     * 参数 : params{
     *      productInfo : O{
     *          阿里返回的数据包
     *      }
     *      vsPath : {
     *          type 类型 : [vsPath 图片保存路径]
     *      }
     * }
     * account
     * 返回值 : {
     *      state : 状态码
     *      info : 状态说明
     * }
     * 作者 : Zoro.Zhu
    **/
    public static function setProduct($params, $account = '') {
        if(core_base::trans()) {
            if(empty($params['productInfo'])){
                $result = array('state' => 300, 'info' => '重要参数缺失');
            }else{
                $productId = $params['productInfo']['productID'];
                $state = !empty($params['productState']) ? $params['productState'] : 20;
                $json = addslashes(json_encode($params));
                L::sql("INSERT INTO `ali_product`(
                  `productId`, `createDate`, `data`,`state`,`account`
                ) VALUES (
                  '{$productId}', '" . date('Y-m-d H:i:s') . "', '{$json}', '{$state}', '{$account}'
                ) ON DUPLICATE KEY UPDATE 
                  `data` = VALUES(`data`)");
                L::sql("DELETE FROM 
                  `ali_product_detail` 
                WHERE 
                  `productId` = '{$productId}'");
                $productSql = "INSERT INTO `ali_product_detail`(
                      `productId`, `specId`, `aliSku`, `aliData`, `aliStock`, `sku`
                    ) VALUES ";
//                $attrSql = "INSERT INTO `ali_product_attr`(`attrVal`, `pddSpecId`, `addTime`) VALUES ";
                foreach($params['productInfo']['skuInfos'] as $sku){
                    $json = addslashes(json_encode($sku));
                    $productSql .= " ( '{$productId}', '{$sku['specId']}', '{$sku['skuId']}', '{$json}', '{$sku['amountOnSale']}', 'VS{$sku['skuId']}'),";
//                    foreach($sku['attributes'] as $attr){
//                        $attrSql .= " ( '{$attr['attributeValue']}', '', '" . date('Y-m-d H:i:s') . "'),";
//                    }
                }
//                L::sql(trim($attrSql, ',') . " ON DUPLICATE KEY UPDATE `attrVal` = VALUES(`attrVal`)");
                L::sql(trim($productSql, ','));
                //非必填，如果传了就记录
                if(!empty($params['vsPath'])){
                    $imgSql = "INSERT INTO `ali_product_img`(`productId`, `vsPath`, `type`) VALUES ";
                    foreach ($params['vsPath'] as $type => $path) {
                        foreach($path as $vsPath){
                            $imgSql .= " ('{$productId}', '{$vsPath}', '{$type}'),";
                        }
                    }
                    $imgSql = trim($imgSql, ',') . " ON DUPLICATE KEY UPDATE 
                        `vsPath` = VALUES(`vsPath`)";
                    L::sql($imgSql);
                }

            }
        }else{
            $result = array('state' => 500, 'info' => '未开启事务');
        }
        empty($result) && $result = array('state' => 200, 'info' => '操作正常');
        core_base::trans($result);
        return $result;
    }

    /**
     * 描述 ：修改pdd图片链接
     * 参数 ：{
     *      productId ：产品ID
     *      vsPath ：系统图片路径
     *      pddUrl ：pdd图片地址
     * }
     * 返回值 : {
     *      state : 状态码
     *      info : 状态说明
     * }
     * 作者 ：Zoro.Zhu
    **/
    public static function updProductImg($params){
        if(core_base::trans()) {
            if(empty($params['productId']) || empty($params['vsPath']) || empty($params['pddUrl'])){
                $result = array('state' => 300, 'info' => '重要参数缺失');
            }else{
                $sql = "UPDATE `ali_product_img` 
                SET 
                  `pddUrl` = '{$params['pddUrl']}' 
                WHERE
                    `productId` = '{$params['productId']}' 
                AND `vsPath` = '{$params['vsPath']}'";
                L::sql($sql);
            }
        }else{
            $result = array('state' => 500, 'info' => '未开启事务');
        }
        empty($result) && $result = array('state' => 200, 'info' => '操作正常');
        core_base::trans($result);
        return $result;
    }

    /**
     * 描述 ：更新产品属性的specId
     * 参数 ：{
     *      attrVal 产品属性名称 ：specId 拼多多属性
     * }
     * 作者 ：Zoro.Zhu
    **/
    public static function updAttrSpec($params){
        if(core_base::trans()) {
            $data = date('Y-m-d H:i:s');
            $attrSql = "INSERT INTO `ali_product_attr`(`attrVal`, `pddSpecId`, `addTime`) VALUES ";
            foreach($params as $key => $val){
                $attrSql .= " ('{$key}','{$val}', '{$data}'),";
            }
            L::sql(trim($attrSql, ',') . " ON DUPLICATE KEY UPDATE `pddSpecId` = VALUES(`pddSpecId`)");
        }else{
            $result = array('state' => 500, 'info' => '未开启事务');
        }
        empty($result) && $result = array('state' => 200, 'info' => '操作正常');
        core_base::trans($result);
        return $result;
    }

}